//
//  MainViewController.m
//  RecreationGame
//
//  Created by appying_wxj on 15/1/22.
//  Copyright (c) 2015年 appying_wxj. All rights reserved.
//

#import "MainViewController.h"
#import "GameShowViewController.h"
@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_mainTableView;
    NSMutableArray *_titleArray;
    NSMutableArray *_objectIdArray;
}

@end

@implementation MainViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor colorWithRed:0.78 green:0.14 blue:0.24 alpha:0.8];
    [self getMainData];
    [self initMainViewUI];
    
}
// 创建界面
-(void)initMainViewUI{
    
//    _titleArray = @[@"侦探游戏",@"幼儿游戏",@"儿童游戏",@"益智游戏",@"整人游戏",@"团队游戏",@"聚会游戏",@"杀人游戏",@"体育游戏",@"晚会游戏",@"心理游戏"];
    
//    // 顶部的展示图片
//    UIScrollView *topScrollView = [[UIScrollView alloc] init];
//    topScrollView.backgroundColor = [UIColor colorWithRed:0.23 green:0.8 blue:0.96 alpha:1];
//    topScrollView.frame = CGRectMake(0, 64, kwidth, 140);
//    [self.view addSubview:topScrollView];
    
    [self.navBG setHidden:YES];
    
    
    
    UIImageView *titleImage = [[UIImageView alloc] init];
    titleImage.frame = CGRectMake((kwidth-203)/2, 36, 203, 40);
    titleImage.image = [UIImage imageNamed:@"image_title"];
    [self.view addSubview:titleImage];
    
    UIImageView *hengImage = [[UIImageView alloc] init];
    hengImage.frame = CGRectMake(0, 36+40+31+84-17, kwidth, 17);
    hengImage.image = [UIImage imageNamed:@"image_heng"];
    [self.view addSubview:hengImage];
    
    UIImageView *bigImage = [[UIImageView alloc] init];
    bigImage.frame = CGRectMake((kwidth-246)/2, 36+40+31, 246, 84);
    bigImage.image = [UIImage imageNamed:@"image_bigImage"];
    [self.view addSubview:bigImage];
    
    // 展示分类的按钮
    _mainTableView = [[UITableView alloc] init];
    _mainTableView.frame = CGRectMake(0, 36+40+31+84, kwidth, kHeight-36-40-31-84);
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = (kwidth-140*2)/3+140;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTableView];

    
}
// 获取数据
-(void)getMainData{
    _titleArray = [[NSMutableArray alloc] initWithCapacity:0];
    _objectIdArray = [[NSMutableArray alloc] initWithCapacity:0];
    
    AVQuery *mainQuery = [AVQuery queryWithClassName:@"classify"];
    [mainQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (int i = 0 ; i < objects.count; i++) {
                AVObject *object = [objects objectAtIndex:i];
                [_titleArray addObject:[object objectForKey:@"taxon"]];
                [_objectIdArray addObject:object.objectId];
            }
            [_mainTableView reloadData];
        } else {
            // Log details of the failure
        }
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _titleArray.count/2+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static  NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    else{
        while ([cell.contentView.subviews lastObject] != nil) {
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    if (indexPath.row<_titleArray.count) {
        
        for (int i=0; (indexPath.row < _titleArray.count/2) ? i < 2 : i < _titleArray.count % 2; i++){
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor colorWithRed:0.67 green:0.8 blue:0.35 alpha:1];
            [button setTitle:[_titleArray objectAtIndex:2 * indexPath.row + i] forState:UIControlStateNormal];
            button.frame = CGRectMake((kwidth-140*2)/3+i%2*(140+(kwidth-140*2)/3), (kwidth-140*2)/3, 140, 121);
            button.tag = indexPath.row*2+i+1000;
            [button addTarget:self action:@selector(cellListButtonClick:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:button];
        }
        
    }
    
    return cell;
}
// 点击cell上面的按钮
-(void)cellListButtonClick:(UIButton *)sender{
    GameShowViewController *gameShow = [[GameShowViewController alloc] init];
    gameShow.objectIdStr = [_objectIdArray objectAtIndex:sender.tag-1000];
    [self.navigationController pushViewController:gameShow animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
